﻿using AutoMapper;
using BlogDemo.Core.Interface;
using BlogDemo.Infrastructure.Database;
using BlogDemo.Infrastructure.Dtos;
using BlogDemo.Infrastructure.Repositories;
using FluentValidation;
using FluentValidation.AspNetCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;

namespace BlogDemo.Api
{
    public class StartupDevelopment
    {
        private readonly IConfiguration _configuration;
        public StartupDevelopment(IConfiguration configuration)
        {
            _configuration = configuration;
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc()
                .AddFluentValidation();
            services.AddHsts(opt =>
            {
                opt.Preload = true;
                opt.MaxAge = TimeSpan.FromDays(60);
                opt.IncludeSubDomains = true;
                opt.ExcludedHosts.Add("example.com");
                opt.ExcludedHosts.Add("www.example.com");
            });
            services.AddHttpsRedirection(opt =>
            {
                opt.RedirectStatusCode = StatusCodes.Status307TemporaryRedirect;
                opt.HttpsPort = 5001;
            });
            //var myConn = "Data Source=App_Data/BlogDemo.db";
            var myConn = _configuration.GetConnectionString("DefaultConnection");
            //var _myConn = _configuration["ConnectionStrings:DefaultConnection"];
            services.AddDbContext<BlogContext>(opt =>
            {
                opt.UseSqlite(myConn);
            });
            services.AddSingleton<SqlSugarContext>(sp => new SqlSugarContext(myConn));
            services.AddScoped<IPostRepository, PostRepository>();
            services.AddScoped<IUnitOfWork, UnitOfWork>();
            services.AddAutoMapper();
            services.AddTransient<IValidator<PostInputDto>, PostInputDtoValidator>();
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddIdentityServerAuthentication(opt =>
                {
                    opt.ApiName = "api";
                    opt.Authority = "http://localhost:5000";
                    opt.RequireHttpsMetadata = false;
                    opt.SaveToken = true;
                });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            if (env.IsProduction())
            {
                app.UseDeveloperExceptionPage();
            }
            if (env.IsStaging())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseHsts();
            //app.UseHttpsRedirection();
            app.UseMvc();

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
    }
}
